import QtQuick 2.0
import QtMultimedia 5.0

Item {
    id: userSelector
    anchors.top: content.top
    anchors.topMargin: 5
    anchors.left: content.left
    anchors.leftMargin: 5
    width: content.width - 10
    height: content.height - 10
    
    property int userCount: 0
    property int loggedUser

    Grid {
        id: userBtnArea
        anchors.left: parent.left
        anchors.leftMargin: 38
        anchors.top: parent.top
        anchors.topMargin: 60
        width: parent.width - 76
        height: parent.height - 120
        columns: 8
        rows: 3
        spacing: 45
    }
    
    Button {
        anchors.right: parent.right
        anchors.rightMargin: 38
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 60
        img: "images/addnew.png"
        text: "Add New"
        onClicked: {
            cameraRect.visible = true;
            countTxt.text = "3";
            cameraTimer.start();
        }
    }
    
    Rectangle {
        id: cameraRect
        width: 500
        height: 500
        anchors.centerIn: parent
        color: "#FF274E13"
        visible: false
        
        Rectangle {
            anchors.centerIn: parent
            width: parent.width-8
            height: parent.height-8
            color: "#FFFFFFFF"
            clip: true
            VideoOutput {
                anchors.top: parent.top
                anchors.horizontalCenter: parent.horizontalCenter
                width: 1920
                height: parent.height
                source: camera
                focus: visible // to receive focus and capture key events when visible
                Text {
                    id: countTxt
                    anchors.centerIn: parent
                    font.family: "Verdana"
                    font.pointSize: 96
                    text: "3"
                    color: "#FFFFFFFF"
                }
            }
        }
    }
    
    Rectangle {
        id: nameRect
        width: 500
        height: 100
        anchors.centerIn: parent
        color: "#FF274E13"
        visible: false
        Text {
            anchors.centerIn: parent
            font.family: "Verdana"
            font.pointSize: 48
            text: "Say Your Name"
            color: "#FFFFFFFF"
        }
    }
    
    Camera {
        id: camera
        
        imageCapture {
            onImageCaptured: {
                console.log("image captured");
            }
        }
    }
    
    Timer {
        id: countDown
        interval: 1000
        running: false
        repeat: false
        triggeredOnStart: true

        onTriggered: {
            if(!running) {
                if(countTxt.text == "3") { countTxt.text = "2"; countDown.start(); }
                else if(countTxt.text == "2") { countTxt.text = "1"; countDown.start(); }
                else { countTxt.text = ""; }
            }
        }
    }

    Timer {
        id: closeNameTimer
        interval: 2000
        running: false
        repeat: false
        triggeredOnStart: true
        onTriggered: {
            if(!running){
                nameRect.visible = false;
            }
        }
    }
    
    Timer {
        id: addBtnTimer
        interval: 1000
        running: false
        repeat: false
        triggeredOnStart: true
        onTriggered: {
            if(!running){
                nameRect.visible = true;
                closeNameTimer.start();
                startSpeechRecognitionReq("user", 2000);
            }
        }
    }

    Timer {
        id: cameraTimer
        interval: 3300
        running: false
        repeat: false
        triggeredOnStart: true

        onTriggered: {
            if(running) {
                countDown.start();
            }

            else {
                camera.imageCapture.capture();
                faceCapture.takePicture("face"+parseInt(userCount)+".png");
                cameraRect.visible = false;
                addBtnTimer.start();
            }
        }
    }

    Timer {
        interval: 1
        running: true
        repeat: false
        triggeredOnStart: false
        onTriggered: {
            userCount = parseInt(settings.getValue("logins/userCount"));

            for( var i = 0; i < userCount; i++ ) {
                var userName = settings.getValue("logins/" + i + "/userName");
                var url = settings.getValue("logins/" + i + "/url");

                var component = Qt.createComponent("Button.qml");
                var sprite = component.createObject(userBtnArea, {"img": url, "text": userName, "id2": i, "action" : [function() { userSelection.visible = false; clockContent.visible = true; }] });
                //sprite.onClicked = { userSelection.loggedUser id2; myKit.loadSettings(id2); };
                //userSelection

            }
        }
    }

    function addUser(userName) {
        var component = Qt.createComponent("Button.qml");
        var sprite = component.createObject(userBtnArea, {"img": "images/face"+parseInt(userCount)+".png", "text": userName, "id2": userCount, "action" : [function() { userSelection.visible = false; clockContent.visible = true; }] });
        settings.setValue("logins/" + userCount + "/userName", userName);
        settings.setValue("logins/" + userCount + "/url", "images/face" + parseInt(userCount) + ".png");
        userCount = userCount+1;
        settings.setValue("logins/userCount", userCount);
    }
}
